package sweepmine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Interrupt extends JDialog implements ActionListener {
    SweepMine frame;
    Font font = new Font("楷体", 1, 18);
    JButton bon2 = new JButton("退出");
    JLabel label = new JLabel("您的队友中途退出，对局强制结束！");
    JPanel panel;

    // 居中显示
    public void showOnCenter() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        if (frameSize.height > screenSize.height)
            frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
            frameSize.width = screenSize.width;
        setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    public Interrupt(String s, SweepMine frame) {
        this.frame = frame;
        setTitle(s);
        addPart();
        setContentPane(panel);
        setSize(400,200);
        showOnCenter();
        close();
        setModal(true);
        setVisible(true);
    }

    //设置默认关闭方式
    public void close() {
        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                frame.backMain();
                dispose();
            }
        });
    }

    // 添加组件
    public void addPart() {
        panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.blue, 4));
        panel.setLayout(null);

        label.setFont(font);
        label.setBounds(130, 10, 200, 50);
        panel.add(label);

        bon2.setFont(font);
        bon2.addActionListener(this);
        bon2.setMargin(new Insets(0, 0,0 ,0));
        bon2.setBounds(115, 100, 150, 40);
        panel.add(bon2);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bon2) {
            frame.backMain();
            dispose();
        }
    }
}
